package com.pachong.frameworkdemo.demo.httpdemo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.pachong.android.baseuicomponent.CompState;
import com.pachong.android.baseuicomponent.activity.LoadableActivity;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.frameworkdemo.R;
import com.pachong.frameworkdemo.account.Account;
import com.pachong.frameworkdemo.account.AccountManager;
import com.pachong.frameworkdemo.account.ParamKey;
import com.pachong.frameworkdemo.account.RegisterResponce;
import com.pachong.frameworkdemo.account.UrlCenter;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.frameworkdemo.common.http.MyHttpRequest;
import com.pachong.frameworkdemo.common.http.TokenInfo;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class RegisterLoginDemoActivity extends LoadableActivity {

    @Bind(R.id.etUserId)
    EditText etLoginName;

    @Bind(R.id.etCaptcha)
    EditText etCaptcha;

    private final static String APP_ID = "happiness";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        tvUrl.setText(mUrl);
        setSwipeBackEnable(true);
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_register_login_demo, null);
    }

    @OnClick(R.id.btnRegister)
    void onRegisterClicked() {
        Log.e("vivi", "注册按钮被按下");
        register();
    }


    @OnClick(R.id.btnLogin)
    void login() {
        Log.e("vivi", "   login = ");
        IHttpRequest request = new MyHttpRequest(this);
        JSONObject jobj = new JSONObject();
        String loginName = etLoginName.getText().toString();
        Log.e("vivi", "   loginName = " + loginName);
        try {
            jobj.put(ParamKey.LOGIN_NAME, loginName);
            jobj.put(ParamKey.PASSWORD, "123123");
            jobj.put(ParamKey.APP_ID, APP_ID);
            request.post(UrlCenter.TOKEN, jobj, new DataRequestListener<TokenInfo>(TokenInfo.class) {

                @Override
                public void onStart(Context context, String url) {
                    enableProgress(false);
                    super.onStart(context, url);
                }

                @Override
                public void onSuccess(TokenInfo bean) {
                    super.onSuccess(bean);
                    Log.e("vivi", "login onSuccess  bean == " + bean);
                    AccountManager.updateTokenInfo(bean);
                    Log.e("vivi", "login onSuccess start to get user info");
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    Log.e("vivi", "login onFailure  str msg " + strMsg + " errorNo == " + errorNo);
                }
            });
            Log.e("vivi", " onStartLoading  ");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void register() {
        IHttpRequest request = new MyHttpRequest(this);
        JSONObject jobj = new JSONObject();
        String loginName = etLoginName.getText().toString();
        Log.e("vivi", "   loginName = " + loginName);

        String captcha = etCaptcha.getText().toString();

        try {
            jobj.put(ParamKey.LOGIN_NAME, loginName);
            jobj.put(ParamKey.PASSWORD, "123456");
            jobj.put(ParamKey.CAPTCHA, captcha);
            jobj.put(ParamKey.APP_ID, APP_ID);
            jobj.put(ParamKey.REAL_NAME, "real_name");
            jobj.put(ParamKey.SIGN, "sdfsdfs");
            jobj.put(ParamKey.PORTRAIT, "");
            jobj.put(ParamKey.PHONE, loginName);
            jobj.put(ParamKey.GENDER, 1);
            jobj.put(ParamKey.TYPE, 1);
            jobj.put(ParamKey.ROLE, "role");
            Log.e("vivi", "   start register post");
            request.post(UrlCenter.REGISTER, jobj, new DataRequestListener<RegisterResponce>(RegisterResponce.class) {

                @Override
                public void onSuccess(RegisterResponce bean) {
                    super.onSuccess(bean);
                    Log.e("vivi", " onSuccess  registe：r bean == " + bean);
//                    login();
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    Log.e("vivi", " onFailure register： str msg " + strMsg + " errorNo == " + errorNo);
//                    setState(CompState.EMPTY_ERROR);
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @OnClick(R.id.btnGetUserInfo)
    public void onGetUsrInfoClicked() {
        UrlParams url = new UrlParams();
//        url.put("key", "value");
        IHttpRequest request = new MyHttpRequest(this);
        request.get(UrlCenter.USER, url, new DataRequestListener<Account>(Account.class) {

            @Override
            public void onSuccess(Account acct) {
                super.onSuccess(acct);
                Log.e("vivi", " onSuccess  account == " + acct);
                setState(CompState.DATA);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Log.e("vivi", " onFailure  str msg " + strMsg + " errorNo == " + errorNo);
                setState(CompState.EMPTY_ERROR);
            }
        });
    }


    @OnClick(R.id.btnPutUserInfo)
    public void onPutUsrInfoClicked() {
        IHttpRequest request = new MyHttpRequest(this);
        JSONObject jobj = new JSONObject();
        try {
            jobj.put(ParamKey.SIGN, "1111111111111111111111111");
            jobj.put(ParamKey.PORTRAIT, "22222222222222");
            Log.e("vivi", "   start onPutUsrInfoClicked post");
            request.put(UrlCenter.USER, jobj, new DataRequestListener<RegisterResponce>(RegisterResponce.class) {

                @Override
                public void onSuccess(RegisterResponce bean) {
                    super.onSuccess(bean);
                    Log.e("vivi", " onSuccess  put user info ：r bean == " + bean);
//                    login();
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    Log.e("vivi", " onFailure put user info： str msg " + strMsg + " errorNo == " + errorNo);
//                    setState(CompState.EMPTY_ERROR);
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onStartLoading() {

    }

}

